package com.ruoyi.project.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.project.domain.ReqShedule;
import com.ruoyi.project.mapper.RequirementMapper;
import com.ruoyi.project.domain.Requirement;
import com.ruoyi.project.service.IRequirementService;

/**
 * 需求Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-06
 */
@Service
public class RequirementServiceImpl implements IRequirementService 
{
    @Autowired
    private RequirementMapper requirementMapper;

    /**
     * 查询需求
     * 
     * @param reqId 需求主键
     * @return 需求
     */
    @Override
    public Requirement selectRequirementByReqId(Long reqId)
    {
        return requirementMapper.selectRequirementByReqId(reqId);
    }

    /**
     * 查询需求列表
     * 
     * @param requirement 需求
     * @return 需求
     */
    @Override
    public List<Requirement> selectRequirementList(Requirement requirement)
    {
        return requirementMapper.selectRequirementList(requirement);
    }

    /**
     * 新增需求
     * 
     * @param requirement 需求
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRequirement(Requirement requirement)
    {
        int rows = requirementMapper.insertRequirement(requirement);
        insertReqShedule(requirement);
        return rows;
    }

    /**
     * 修改需求
     * 
     * @param requirement 需求
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRequirement(Requirement requirement)
    {
        requirementMapper.deleteReqSheduleByReqId(requirement.getReqId());
        insertReqShedule(requirement);
        return requirementMapper.updateRequirement(requirement);
    }

    /**
     * 批量删除需求
     * 
     * @param reqIds 需要删除的需求主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRequirementByReqIds(Long[] reqIds)
    {
        requirementMapper.deleteReqSheduleByReqIds(reqIds);
        return requirementMapper.deleteRequirementByReqIds(reqIds);
    }

    /**
     * 删除需求信息
     * 
     * @param reqId 需求主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRequirementByReqId(Long reqId)
    {
        requirementMapper.deleteReqSheduleByReqId(reqId);
        return requirementMapper.deleteRequirementByReqId(reqId);
    }

    /**
     * 新增需求进度信息
     * 
     * @param requirement 需求对象
     */
    public void insertReqShedule(Requirement requirement)
    {
        List<ReqShedule> reqSheduleList = requirement.getReqSheduleList();
        Long reqId = requirement.getReqId();
        if (StringUtils.isNotNull(reqSheduleList))
        {
            List<ReqShedule> list = new ArrayList<ReqShedule>();
            for (ReqShedule reqShedule : reqSheduleList)
            {
                reqShedule.setReqId(reqId);
                list.add(reqShedule);
            }
            if (list.size() > 0)
            {
                requirementMapper.batchReqShedule(list);
            }
        }
    }
}
